package com.zhtools;

import java.math.BigDecimal;
import java.math.RoundingMode;

public final class DecimalUtils {
    private static final int DEFAULTSCALE = 2;
    private static final RoundingMode DEFAULTROUNDINGMODE = RoundingMode.DOWN;

    public static BigDecimal multiply(BigDecimal number1, BigDecimal number2) {
        return multiply(number1, number2, DEFAULTSCALE, DEFAULTROUNDINGMODE);
    }

    public static BigDecimal divide(BigDecimal number1, BigDecimal number2) {
        return divide(number1, number2, DEFAULTSCALE, DEFAULTROUNDINGMODE);
    }

    public static BigDecimal add(BigDecimal number1, BigDecimal number2) {
        return add(number1, number2, DEFAULTSCALE, DEFAULTROUNDINGMODE);
    }

    public static BigDecimal sub(BigDecimal number1, BigDecimal number2) {
        return sub(number1, number2, DEFAULTSCALE, DEFAULTROUNDINGMODE);
    }

    private static BigDecimal multiply(BigDecimal number1, BigDecimal number2, int newScale, RoundingMode roundingMode) {
        BigDecimal result = number1.multiply(number2);
        result.setScale(newScale, roundingMode);
        return result;
    }

    private static BigDecimal divide(BigDecimal number1, BigDecimal number2, int newScale, RoundingMode roundingMode) {
        BigDecimal result = number1.divide(number2, newScale, roundingMode);
        return result;
    }

    private static BigDecimal add(BigDecimal number1, BigDecimal number2, int newScale, RoundingMode roundingMode) {
        BigDecimal result = number1.add(number2);
        return result.setScale(newScale, roundingMode);
    }

    private static BigDecimal sub(BigDecimal number1, BigDecimal number2, int newScale, RoundingMode roundingMode) {
        BigDecimal result = number1.subtract(number2);
        return result.setScale(newScale, roundingMode);
    }
}
